package cn.ubeta.b2c.user.order;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import cn.ubeta.b2c.R;
import cn.ubeta.b2c.api.ApiOrder;
import cn.ubeta.b2c.bean.OrderInfo;
import cn.ubeta.b2c.cart.CheckoutActivity;
import cn.ubeta.b2c.common.ListFragment;
import cn.ubeta.b2c.common.ListFragmentConfig;
import cn.ubeta.b2c.widget.recyclerview.BaseViewHolder;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static cn.ubeta.b2c.product.ProductListFragment.setResImage;

/**
 * Created by denny on 16/9/6.
 */

public class OrderFragment extends ListFragment<OrderInfo> {

    public static final String ARG_TYPE = "order_type";

    private int mType;
    private ProgressDialog mProgressDialog;

    public static OrderFragment newInstance(int type) {
        OrderFragment fragment = new OrderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mType = getArguments().getInt(ARG_TYPE);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected ListFragmentConfig getConfig() {
        return new ListFragmentConfig(R.layout.item_order, 1);
    }

    @Override
    protected void bindItemView(BaseViewHolder helper, OrderInfo item) {
        // 状态
        long time = item.getTime();
        int status = OrderInfo.STATUS_UNPAY;
        if (time == OrderInfo.PAID_FLAG) {
            status = OrderInfo.STATUS_PAID;
        } else if (System.currentTimeMillis() - time > OrderInfo.ORDER_TIMEOUT_MS) {
            status = OrderInfo.STATUS_TIMEOUT;
        }
        TextView tvStatus = helper.getView(R.id.tv_order_status);
        TextView tvPay = helper.getView(R.id.tv_order_pay);
        switch (status) {
            case OrderInfo.STATUS_PAID:
                tvStatus.setText("已付款（请去京东APP查看物流信息）");
                tvPay.setVisibility(GONE);
                break;
            case OrderInfo.STATUS_TIMEOUT:
                tvStatus.setText("已取消");
                tvPay.setVisibility(GONE);
                break;
            case OrderInfo.STATUS_UNPAY:
                tvStatus.setText("等待付款");
                tvPay.setVisibility(VISIBLE);
                tvPay.setOnClickListener(v -> gotoJdPay(item));
                break;
        }

        // 商品图片
        SimpleDraweeView img = helper.getView(R.id.iv_product);
        setResImage(img, item.getProductImg());

        // 商品标题
        String name = item.getGoodsName();
        if (!TextUtils.isEmpty(name)) {
            ((TextView) helper.getView(R.id.tv_name)).setText(name);
        }
        // 待支付金额
        TextView tvPayment = helper.getView(R.id.tv_order_payment);
        tvPayment.setText(String.format("￥%.2f", item.getNeedMoney()));

        // 下单时间
        TextView tvTime = helper.getView(R.id.tv_time);
        tvTime.setText(item.getFormatedTime());

        // 点击跳转订单详情
        //helper.getConvertView().setOnClickListener(v -> OrderDetailActivity.gotoOrderDetail(getActivity(), item.getOrderId()));
        // 查看物流
        //helper.getView(R.id.tv_check_logistics).setOnClickListener(v -> LogisticsActivity.gotoLogistics(getActivity(), item.getOrderId()));
    }

    @Override
    protected void loadData(int page) {
        ApiOrder.getOrder(mType, page, getNetworkCallback());
    }

    /**
     * 重新加载
     */
    public void reload() {
        loadNew();
    }

    /**
     * 跳转京东支付
     * @param info
     */
    private void gotoJdPay(OrderInfo info) {
        // 等待
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getContext());
            mProgressDialog.setTitle("请稍等");
        }
        CheckoutActivity.gotoPay(getActivity(), mProgressDialog, info);
    }
}
